package com.iretailer.report.quartz.horizon.common;

import com.alibaba.fastjson.JSON;
import com.iretailer.report.quartz.horizon.RequestParam;
import com.iretailer.report.quartz.horizon.Settings;
import com.iretailer.report.util.TimeUtil;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;

import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.SortedMap;
import java.util.SortedMap;

/**
 * @Auther: xjw
 * @Date: 2019/10/12 0012 17:00
 * @Description:
 */
public class ClientHttpMethod {

    private Signer signer;


    private ClientHttpMethod(Signer signer) {
        this.signer = signer;
    }

    public static ClientHttpMethod getHttpMethod(Signer signer) {
        return new ClientHttpMethod(signer);
    }

    public <T> ResponseEntity<T> httpGet(Class<T> t, String api, SortedMap<String, String> headersParams, SortedMap<String, String> params) throws Exception {
        URI uri = getUrl(HttpMethod.GET, api, headersParams, params);
        return CreatRestTemplate.getRestTemplate().getForEntity(uri, t);
    }

    public <T> ResponseEntity<T> httpDelete(Class<T> t, String api, SortedMap<String, String> headersParams, SortedMap<String, String> params) throws Exception {
        URI uri = getUrl(HttpMethod.DELETE, api, headersParams, params);
        return CreatRestTemplate.getRestTemplate().exchange(uri, HttpMethod.DELETE, null, t);
    }


    public <T> ResponseEntity<T> httpPOST(Class<T> t, String api, SortedMap<String, String> headersParams, SortedMap<String, String> params, String body) throws Exception {
        HttpHeaders headers = new HttpHeaders();
        String authorization = this.signer.Sign(HttpMethod.POST, api, params, headersParams);
        headers.add("content-type", "application/json");
        headers.add("authorization", authorization);
        HttpEntity<String> formEntity = new HttpEntity<String>(body, headers);
        String url = String.format("http://%s%s", headersParams.get(Settings.HOST), api);
//        String url = getUrl(HttpMethod.GET, api, headersParams, params);
        return CreatRestTemplate.getRestTemplate().exchange(url, HttpMethod.POST, formEntity, t);
    }

    public <T> ResponseEntity<T> httpPUT(Class<T> t, String api, SortedMap<String, String> headersParams, SortedMap<String, String> params, String body) throws Exception {
        HttpHeaders headers = new HttpHeaders();
        String authorization = this.signer.Sign(HttpMethod.PUT, api, params, headersParams);
        headers.add("Content-Type", "application/json");
        headers.add("authorization", authorization);
        HttpEntity<String> formEntity = new HttpEntity<String>(body, headers);
        String url = String.format("http://%s%s", headersParams.get(Settings.HOST), api);
        return CreatRestTemplate.getRestTemplate().exchange(url, HttpMethod.PUT, formEntity, t);
    }


    public URI getUrl(HttpMethod httpMethod, String api, SortedMap<String, String> headersParams, SortedMap<String, String> params) throws Exception {
        String authorization = this.signer.Sign(httpMethod, api, params, headersParams);
        String param = Signer.getCanonicalQueryString(params, Signer.DEFAULT_ENCODING);
        return URI.create(String.format("http://%s%s?%s&authorization=%s", headersParams.get(Settings.HOST), api, param, authorization));
    }
}
